<?php
namespace Admin\Model;
use Think\Model;
class YyusertoprizeModel extends Model {
    protected $autoCheckFields =false;
    protected $table ='yyusertoprize';
	//指定用户
	public function get_list($page=1,$pagesize=10,$where_data="",$order="u.status asc,u.ctime desc"){


      
        $where="";
		foreach ($where_data as $key => $value) {
			 $where.=" and  u.".$key." like '%".$value."%'";
		}

       

        $where_str='1=1  '.$where;

		$field="u.*,p.prizes_name";
		$info=M('Yyusertoprize')->alias('u')->join(' left JOIN tp_yyprizes as p  ON p.prizes_id = u.prizes_id')->field($field)->where($where_str)->order($order)->page($page.','.$pagesize)->select();

// print_r(M('Yyusertoprize')->_SQL());exit;

		foreach ($info as $k => $v) {
			
			$info[$k]['ctime']=date("Y-m-d H:i:s",$v['ctime']);
			if($info[$k]['status']==1)
			{
				$info[$k]['status_str']="已使用";
				$info[$k]['used_time']=date("Y-m-d H:i:s",$v['used_time']);
			}
			else if($info[$k]['status']==2)
			{
				$info[$k]['status_str']="取消";
				$info[$k]['used_time']=0;
			}
			else
			{
				$info[$k]['status_str']="未使用";
				$info[$k]['used_time']=0;
			}

		}
		$count=M('Yyusertoprize')->alias('u')->join(' tp_yyprizes as p  ON p.prizes_id = u.prizes_id')->where($where_str)->count();


		$Page=new\Think\Page($count,$pagesize);	

		//分页跳转的时候保证查询条件

		foreach($where_data as $key=>$val) {
		    $Page->parameter[$key]   =   urlencode($val);
		}

		$show=$Page->show();
		return array("list"=>$info,"page"=>$show);
	}

   //参与码列表
	public function get_code_list($page=1,$pagesize=10,$where_data="",$order="u.used_time desc,u.code_id desc"){


      
        $where="";
		foreach ($where_data as $key => $value) {
			 $where.=" and  u.".$key." like '%".$value."%'";
		}

       

        $where_str='1=1  '.$where;

		$field="u.*,g.prizes_name,g.name,g.address,g.phone,g.wechat";
		$info=M('yycode')->alias('u')->join(' left JOIN tp_yyprizes_log as g  ON g.code_id = u.code_id')->field($field)->where($where_str)->order($order)->page($page.','.$pagesize)->select();

// print_r(M('Yyusertoprize')->_SQL());exit;

		foreach ($info as $k => $v) {
			
			if($info[$k]['status']==1)
			{
				$info[$k]['status_str']="已使用";
				if($v['used_time'])
				$info[$k]['used_time']=date("Y-m-d H:i:s",$v['used_time']);
			}
			else
			{
				$info[$k]['status_str']="未使用";
				$info[$k]['used_time']=0;
			}

		}
		$count=M('yycode')->alias('u')->join(' left JOIN tp_yyprizes_log as g  ON g.code_id = u.code_id')->where($where_str)->count();


		$Page=new\Think\Page($count,$pagesize);	

		//分页跳转的时候保证查询条件

		foreach($where_data as $key=>$val) {
		    $Page->parameter[$key]   =   urlencode($val);
		}

		$show=$Page->show();
		return array("list"=>$info,"page"=>$show);
	}
	

}